 <?php

            $slug = sanitize_title($_POST['term_name']);

            $slug_exists = get_category_by_slug($slug);

            //check slug

            $i = 0;

            while($slug_exists->term_id > 0)  {

                $slug = sanitize_title($_POST['term_name']).'-'. $i++;

                $slug_exists = get_category_by_slug($slug); 

            }

            if($_POST['btnSubmit']){                  

                  $term_id = 0;

                  $parent = $_POST['parent'];

                  if(!term_exists($_POST['term_name'],$_POST['group'], $parent )) {

                    $term = wp_insert_term($_POST['term_name'], $_POST['group'], array(

                                                'description'=> $_POST['description'],

                                                'slug' => $slug_exists,

                                                'parent'=> $_POST['parent']

                                            ) ); 

                    $term_id = $term['term_id'];

                    

                    

                  }  else {

                    $args = array(  'description'=> $_POST['description'],

                                    'name' => $_POST['term_name'],

                                    'slug' => $slug_exists,

                                    'parent'=> $_POST['parent']

                                );

                                

                     wp_update_term( $_POST['hidID'], $_POST['group'], $args );

                     

                     $term_id = $_POST['hidID'];

                  }

                  

                  $sql = "UPDATE  ".$wpdb->prefix."terms                                

                                SET weight = ".$_POST['order'].", approve=".$_POST['approved']."                                

                            WHERE term_id=".$term_id;

                  $wpdb->query($sql); 

                  error_('Add New/Update OK!','confirm');   

                  wp_redirect_to('?page=accm_Category_Management&tabmenu='.$_GET['tabmenu'].'&taxonomy='.$_GET['taxonomy'].'&cat_id='.$_GET['cat_id'],0);           

            }

            if($_POST['btnDelete']){

                $cat_IDs = $_POST['chk_item'];

            }

            //approve or denied

            if($_POST['btnApprove'] || $_POST['btnDeny']){   

                

                $approve = 0;

                if($_POST['btnApprove']) $approve = 1;

                

                $cat_IDs = $_POST['chk_item'];

                $sql = "UPDATE  ".$wpdb->prefix."terms SET 

                                approve=".$approve."

                            WHERE term_id in (". implode(',',$cat_IDs).")";

                $wpdb->query($sql); 

                error_('Update OK!','confirm'); 

            }

    ?>

    <form action="" method="post" id="frmCompany"  name="frmCompany"> 

        <?php if( $_GET['tabmenu']=='category' || $_GET['tabmenu']=='') { ?>

        Select: 	<select name="group_select" id="group_select">

                            <option value="">-Select--</option>

                            <option value="bs_category">Category</option>

                            <option value="neighborhood">Neighborhood</option>

                            <option value="hotelregion">Hotel Region</option>

                            <option value="cuisine">Cuisine</option>

                            <option value="pricerange">Price Range</option>

                        </select>      

        <br />&nbsp;<br />      

        <?php } ?>    	 	 	 	 	

        <div id="table-company">            

            <div class="header row">

                <div class="col1 col"><input type="checkbox" id="chk_all" name="chk_all" /></div>

                <div class="col2 col">ID</div>

                <div class="col3 col">Name</div>

                <div class="col5 col">Group</div>

                <div class="col5 col">Order</div>

                <div class="col5 col">Approved</div>                 

                 <?php if( ($_GET['tabmenu']=='category' || $_GET['tabmenu']=='')

                         && ($_GET['taxonomy']=='bs_category' || $_GET['taxonomy']=='') ) { ?> <div class="col7 col">SubCategory</div>  <?php } ?>

                <div class="col7 col">Edit</div>

                 <?php if( ($_GET['tabmenu']=='category' || $_GET['tabmenu']=='')

                         && ($_GET['taxonomy']=='bs_category' || $_GET['taxonomy']=='')) { ?><div class="col7 col">Fields</div> <?php } ?>

            </div>

            <?php

            if( $_GET['taxonomy'] != '') 

                $where = " AND termx.taxonomy='". $_GET['taxonomy'] ."' ";

            else

                $where = " AND termx.taxonomy='bs_category' ";

            $where .= " AND termx.parent=". ($_GET['cat_id'] > 0 ? $_GET['cat_id']:0);

            

            $sql = "SELECT * FROM  ".$wpdb->prefix."terms terms

                    INNER JOIN ".$wpdb->prefix."term_taxonomy termx ON termx.term_id = terms.term_id

                    WHERE termx.taxonomy in ('bs_category','neighborhood','hotelregion','cuisine','pricerange') $where

                    ORDER BY termx.taxonomy, terms.weight";

            $db_terms = $wpdb->get_results($sql);

           

            if(count($db_terms) > 0) {

                foreach($db_terms as $term) {               

                ?>

            <div class="row">

                <div class="col1 col"><input class="chk_item" type="checkbox" id="chk_item<?php print $term->term_id;?>" name="chk_item[]" value="<?php print $term->term_id; ?>" /></div>

                <div class="col2 col"><?php print $term->term_id;?></div>

                <div class="col3 col"><?php print $term->name;?></div>

                <div class="col5 col"><?php print $term->taxonomy;?></div>

                <div class="col5 col"><?php print $term->weight;?></div>

                <div class="col5 col"><?php print $term->approve==1?'Yes':'No';?></div>           

                <?php if( ($_GET['tabmenu']=='category' || $_GET['tabmenu']=='')

                         && ($_GET['taxonomy']=='bs_category' || $_GET['taxonomy']=='')) { ?>      

                <div class="col7 col">                   

                    <a href="?page=<?php print $_GET['page'];?>&tabmenu=subcategory&cat_id=<?php print $term->term_id; ?>&taxonomy=<?php print $term->taxonomy;?>">View</a>                   

                </div>     

                <?php } ?>   

                <div class="col7 col"><a href="?page=<?php print $_GET['page'];?>&tabmenu=<?php print $_GET['tabmenu'];?>&act=update&id=<?php print $term->term_id;?>&taxonomy=<?php print ($_GET['taxonomy']==''? $term->taxonomy:$_GET['taxonomy']);?>&cat_id=<?php print $_GET['cat_id']; ?>#form-update">Edit</a></div>

                <?php if( ($_GET['tabmenu']=='category' || $_GET['tabmenu']=='')

                         && ($_GET['taxonomy']=='bs_category' || $_GET['taxonomy']=='')) { ?>

                <div class="col7 col"><a href="?page=<?php print $_GET['page'];?>&tabmenu=categoryfield&act=field&cat_id=<?php print $term->term_id; ?>&taxonomy=<?php print $term->taxonomy;?>#form-update">Edit</a></div>              

                <?php } ?>

            </div>

             <?php }  //for

             } else {

                print '<div class="col" style="width: 100%; text-align: center;">No data!</div>';

             }?>

        </div>

        <div class="footer">           

            <input type="button" value="Add Category" id="btnAddCategory" name="btnAddCategory" class="button">

            <input type="submit" value="Approve" id="btnApprove" name="btnApprove" class="button">

            <input type="submit" value="Deny" id="btnDeny" name="btnDeny" class="button">

            <!-- <input type="submit" value="Delete" id="btnDelete" name="btnDelete" class="button"> -->

        </div>

    </form> 

    <br clear="all">

     <a name="form-update"></a>

<?php

       //update commnay       

       if(($_GET['act']=='update' && ($_GET['id'])) || $_GET['act']=='add') {        



             $term  = get_term($_GET['id'],$_GET['taxonomy']);               

             $sql = "SELECT t.weight, t.approve, tx.description FROM ".$wpdb->prefix."terms t

                        INNER JOIN ".$wpdb->prefix."term_taxonomy tx ON tx.term_id = t.term_id

                        WHERE t.term_id=".$_GET['id'];             

             $db_term = $wpdb->get_row($wpdb->prepare($sql));  

        ?>     

    <br clear=all /> 

    <h2>AddNew/Update Category</h2>

    <div class="wrap" id="register_user" style="width: 600px">     

        <form enctype="multipart/form-data" action="" method="post" name="frmField" id="frmField">	

            <table width="100%" cellspacing="5" cellpadding="0" border="0">

            <tbody><tr>

            	<td valign="top" align="left" colspan="2">&nbsp;            	

               	</td>

            </tr>   

            

            <tr valign="top" width="100%"><td align="right" colspan="2"><i><font color="#bd0000">*</font> Indicates Required Information</i></td></tr>        

    		<tr><td colspan="2" class="formheader1">Field Information</td></tr>

            <tr valign="top"><td align="right" style="width:28%;"><label><font color="#bd0000">*</font> Name:</label></td><td align="left" style="width:72%;">        	

                    	<input size="40" type="text" class="required" value="<?php print $term->name;?>" name="term_name" id="term_name">                

                </td></tr>    

            <tr valign="top"><td align="right" style="width:28%;"><label>Parent:</label></td><td align="left" style="width:72%;"> 

                        <?php                            

                            //$categories = get_terms( 'bs_category');                            

                            $categories = get_terms( 'bs_category', 'orderby=name&hide_empty=0&parent=0' );                            

                        ?>       	

                    	<select name="parent" id="parent">

                            <option value="0">-Select--</option>

                            <?php foreach($categories as $cat) { ?>

                                  <option value="<?php print $cat->term_id ;?>" 

                                  <?php print (($term->parent == $cat->term_id  || $_GET['cat_id'] == $cat->term_id) ?"selected=selected":"" ) ?> >&nbsp;&nbsp;<?php print $cat->name;?></option>                               

                            <?php } ?>

                        </select>                

            	</td></tr>    

            <?php

                $arr_cat = array(

                                'bs_category' => 'Category',

                                'neighborhood' => 'Neighborhood',

                                'hotelregion' => 'Hotel Region',

                                'cuisine' => 'Cuisine',

                                'pricerange' => 'Price Range'

                            );

            ?>

            <tr valign="top"><td align="right" style="width:28%;"><label><font color="#bd0000">*</font> Group:</label></td><td align="left" style="width:72%;">        	

                    	<select name="group" id="group">

                            <option value="">-Select--</option>

                            <?php foreach($arr_cat as $key => $name) { ?>

                            <option value="<?php print $key; ?>" 

                                <?php print (($term->taxonomy == $key || $_GET['taxonomy'] == $key) ?"selected=selected":"" ) ?>>

                                <?php print $name; ?>&nbsp;&nbsp;</option>

                            <?php } ?>

                        </select>                

            	</td></tr>

             <tr valign="top"><td align="right" style="width:28%;"><label><font color="#bd0000">*</font> Desciption:</label></td><td align="left" style="width:72%;">        	

                    	<textarea id="description" name="description" rows="5" style="width:  480px;"><?php print $db_term->description;?></textarea>           

            	</td></tr>

              <tr valign="top"><td align="right" style="width:28%;"><label><font color="#bd0000">*</font> Order:</label></td><td align="left" style="width:72%;">        	

                    	<input type="text" value="<?php print $db_term->weight;?>" name="order" id="order">                

            	</td></tr>

             <tr valign="top"><td align="right" style="width:28%;"><label><font color="#bd0000">*</font> Approve:</label></td><td align="left" style="width:72%;">        	

                    	<input type="checkbox" <?php print ($db_term->approve==1? 'checked=true' : '') ;?> value="1" name="approved" id="approved">                

            	</td></tr>

             <tr valign="top" width="100%"><td valign="top" align="right" colspan="2">

             <input type="hidden" value="<?php print $_GET['id'];?>" id="hidID" name="hidID" />

			<input type="button" id="btnReset" name="btnResett" value="Reset"  class="button">&nbsp;&nbsp;<input type="submit" id="btnSubmit" name="btnSubmit" value="Submit"  class="button">

            </td></tr>        

        </tbody></table>

        </form>

        

   </div>

</div>

<?php } ?>

 <script type="text/javascript">

        (function($){

            $(document).ready(function() {                

                 $('#btnAddCategory').click(function(){                        

                        window.location = "?page=<?php print $_GET['page'];?>&tabmenu=<?php print $_GET['tabmenu'];?>&act=add&cat_id=<?php print $_GET['cat_id'];?>&taxonomy=<?php print $_GET['taxonomy'];?>#form-update";

                 });  

            

                $('#chk_all').click(function(){               

                    if (this.checked) 

                        $('.chk_item').attr('checked',true);

                    else

                       $('.chk_item').removeAttr('checked'); 

                });  

                $('#btnDelete').click(function(){

                     chk = false;

                     $('.chk_item').each(function(){

                        if(this.checked) chk = true;

                     });

                     if (!chk) {

                        alert('Please choose item to delete!');

                        return false;

                     } else {

                        return confirm('Are you sure to delete the selected item(s) ?');

                     }

                }); 

                $('#btnApprove').click(function(){

                     chk = false;

                     $('.chk_item').each(function(){

                        if(this.checked) chk = true;

                     });

                     if (!chk) {

                        alert('Please choose item to delete!');

                        return false;

                     } else {

                        return true;

                     }

                }); 

                $('#btnDeny').click(function(){

                     chk = false;

                     $('.chk_item').each(function(){

                        if(this.checked) chk = true;

                     });

                     if (!chk) {

                        alert('Please choose item to delete!');

                        return false;

                     } else {

                        return true;

                     }

                }); 

                $('#group_select').change(function(){

                    window.location = '?page=accm_Category_Management&tabmenu=category&taxonomy=' + $(this).val();

                });

            }); 

        })(jQuery);

</script>    